编写 HTTP 客户端

创建 HTTP 客户端

使用默认选项创建一个HttpClient实例,如下所示:

  1. HttpClient client = vertx.createHttpClient();

如果您想要在创建时配置客户端的选项,如下所示:

  1. HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);
  2. HttpClient client = vertx.createHttpClient(options);

发出请求

Http 客户端非常灵活,可以用各种方式请求。

http 客户端经常需要将许多请求发送到相同的主机/端口 。为了避免每次发出请求需要重复配置主机/端口,您可以配置客户端的默认主机/端口:

  1. HttpClientOptions options = new HttpClientOptions().setDefaultHost("wibble.com");
  2. // Can also set default port if you want...
  3. HttpClient client = vertx.createHttpClient(options);
  4. client.getNow("/some-uri", response -> {
  5. System.out.println("Received response with status code " + response.statusCode());
  6. });

或者可以指定单个请求的主机/端口。

  1. HttpClient client = vertx.createHttpClient();
  2. // Specify both port and host name
  3. client.getNow(8080, "myserver.mycompany.com", "/some-uri", response -> {
  4. System.out.println("Received response with status code " + response.statusCode());
  5. });
  6. // This time use the default port 80 but specify the host name
  7. client.getNow("foo.othercompany.com", "/other-uri", response -> {
  8. System.out.println("Received response with status code " + response.statusCode());
  9. });
没有请求body简单的请求

通常情况下,你会想要与没有请求body的 HTTP 请求。这通常是 HTTP GET,OPTIONS和HEAD请求。

这是 Vert.x http 客户端的最简单方法,使用前缀与Now的方法。例如getNow

这些方法创建HTTP请求,并在单个方法调用发送,让你提供一个处理程序,将与HTTP响应时调用它回来。

  1. HttpClient client = vertx.createHttpClient();
  2. // Send a GET request
  3. client.getNow("/some-uri", response -> {
  4. System.out.println("Received response with status code " + response.statusCode());
  5. });
  6. // Send a GET request
  7. client.headNow("/other-uri", response -> {
  8. System.out.println("Received response with status code " + response.statusCode());
  9. });
编写普通的requests

不知道想要发送请求方法,运行时才知道。这个用例我们提供通用请求方法例如request,可以在运行时指定 HTTP 方法:

  1. HttpClient client = vertx.createHttpClient();
  2. client.request(HttpMethod.GET, "some-uri", response -> {
  3. System.out.println("Received response with status code " + response.statusCode());
  4. }).end();
  5. client.request(HttpMethod.POST, "foo-uri", response -> {
  6. System.out.println("Received response with status code " + response.statusCode());
  7. }).end("some-data");
编写请求主体(bodies)

有时想要在requests里含有body,或者想写请求的headers。

可以使用post方法,或者普通的request方法

这些方法发送的请求不会立即返回,而是返回HttpClientRequest实例,用于写入body和header。

下面是一些写入body和header的 POST 请求的例子:

  1. HttpClient client = vertx.createHttpClient();
  2. HttpClientRequest request = client.post("some-uri", response -> {
  3. System.out.println("Received response with status code " + response.statusCode());
  4. });
  5. // Now do stuff with the request
  6. request.putHeader("content-length", "1000");
  7. request.putHeader("content-type", "text/plain");
  8. request.write(body);
  9. // Make sure the request is ended when you're done with it
  10. request.end();
  11. // Or fluently:
  12. client.post("some-uri", response -> {
  13. System.out.println("Received response with status code " + response.statusCode());
  14. }).putHeader("content-length", "1000").putHeader("content-type", "text/plain").write(body).end();
  15. // Or event more simply:
  16. client.post("some-uri", response -> {
  17. System.out.println("Received response with status code " + response.statusCode());
  18. }).putHeader("content-type", "text/plain").end(body);

默认utf-8编码:

  1. request.write("some data");
  2. // Write string encoded in specific encoding
  3. request.write("some other data", "UTF-16");
  4. // Write a buffer
  5. Buffer buffer = Buffer.buffer();
  6. buffer.appendInt(123).appendLong(245l);
  7. request.write(buffer);

如果你的 HTTP 请求只写入单个字符串或缓冲区,你可以写它和结束end函数单一调用中的请求。

  1. request.end("some simple data");
  2. // Write buffer and end the request (send it) in a single call
  3. Buffer buffer = Buffer.buffer().appendDouble(12.34d).appendLong(432l);
  4. request.end(buffer);

当你写到一个请求时,write第一次调用会导致写入网络的请求标头。

实际的写操作是异步的之前一段时间后调用已返回, 不是可能发生。

非分块请求正文与 HTTP 请求需要要提供的Content-Length标头。

因此,如果您不使用分块的 HTTP 然后之前,必须设置Content-Length标头写入请求,否则它将太迟了。

如果您正在调用接受字符串或缓冲区的end方法之一然后 Vert.x 将自动计算并设置Content-Length标头在写请求正文之前。

如果您使用的 HTTP 分块Content-Length标头不是必需的所以你不需要计算的前期的大小。